package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjSituationRecordDao;
import cn.topevery.garden.dal.util.QueryWrapper;
import cn.topevery.garden.entity.dto.ProjMoneyDTO;
import cn.topevery.garden.entity.po.ProjSituationRecord;
import cn.topevery.garden.entity.query.ProjSituationRecordQuery;
import cn.topevery.garden.entity.vo.ProjSituationRecordMoneyVO;
import cn.topevery.garden.entity.vo.ProjSituationRecordVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 项目前期情况记录表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Repository
public class ProjSituationRecordDaoImpl extends BaseDaoImpl<ProjSituationRecord> implements ProjSituationRecordDao {

    @Override
    public <P extends DbCsp> List<ProjSituationRecord> getSearch(P para) {
        return null;
    }

    @Override
    public ProjSituationRecord getByProjId(String projId) {
        ProjSituationRecord projSituationRecord = getEntityByColumns(new QueryWrapper<>().eq("c_db_status", 0).eq("c_proj_id", projId));
        return projSituationRecord;
    }

    @Override
    public DataPager<ProjSituationRecordVO> listPage(ProjSituationRecordQuery query) {
        Integer pageIndex = query.pageIndex - 1;
        String table = "t_proj_situation_record tpsr";

        String columns = "tpsr.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tpsr.c_id desc");
        sqlQueryMate
                .where(true, "tpsr.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, query.pageSize, pageIndex, ProjSituationRecordVO.class);
    }

    @Override
    public List<ProjSituationRecordVO> list(ProjSituationRecordQuery query) {
        Integer pageIndex = query.pageIndex - 1;
        String table = "t_proj_situation_record tpsr";

        String columns = "tpsr.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tpsr.c_id desc");
        sqlQueryMate
                .where(true, "tpsr.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(ObjectUtil.isNotEmpty(query.getIds()), "tpsr.c_id", ScriptConditionEnum.In, query.getIds())
        ;

        return selectList(sqlQueryMate, ProjSituationRecordVO.class);
    }

    @Override
    public ProjSituationRecordMoneyVO getEstimateMoney(ProjMoneyDTO moneyDTO) {
        String table = "t_proj_situation_record tpsr";

        String columns = " sum(tpsr.c_estimate_total_money) as c_estimate_total_money, " +
                " sum(tpsr.c_estimate_approval_money) as c_estimate_approval_money ";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tpsr.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(ObjectUtil.isNotEmpty(moneyDTO.getProjId()), "tpsr.c_proj_id", ScriptConditionEnum.Eq, moneyDTO.getProjId())
        ;
        return selectOne(sqlQueryMate, ProjSituationRecordMoneyVO.class);
    }
}
